package com.yangcangclub.pay.tenpay;

import com.yangcangclub.commons.utils.MD5;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

/**
 * Created by user on 2017/2/24.
 */
public class WeChatTool {
    public static String wxSign(SortedMap<String, String> paramMap, String key) throws Exception{
        StringBuffer sb = new StringBuffer();
        Set es = paramMap.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k)
                    && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + key);
        String sign = MD5.getMD5ofStr(sb.toString());
        return sign;
    }

    public  static String getWxXml(Map<String, String> paramMap){
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        for (String key : paramMap.keySet()) {
            if(key.equals("attach") || key.equals("sign") || key.equals("body")){
                sb.append("<" +key + "><![CDATA[").append(paramMap.get(key)).append("]]></"+ key +">");
                continue;
            }
            sb.append("<" +key + ">").append(paramMap.get(key)).append("</"+ key +">");
        }
        sb.append("</xml>");
        return sb.toString();
    }
}
